package com.campus.client.view;

import com.campus.client.controller.TeacherMainController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.User;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;

public class TeacherMainFrame {

    Button changePasswordBtn = new Button("修改密码");
    Button logoutBtn = new Button("退出登录");
    Button teacherInfoBtn = new Button("教师信息");
    Button courseSystemBtn = new Button("课程系统");
    Button libraryBtn = new Button("图书馆系统");
    Button storeBtn = new Button("商店系统");
    Button bankBtn = new Button("校园银行");

    private User user=new User();
    public User getUser() {return user;}
    public void setUser(String Id,String password,String role) {
        this.user.setId(Id);
        this.user.setPassword(password);
        this.user.setRole(role);
        //System.out.print("用户名"+this.user.getId());
    }

    private TeacherMainController controller;


    public void show(Stage primaryStage) throws IOException {
        // 创建主VBox容器
        VBox mainVBox = new VBox();
        System.out.println("用户名"+user.getId());
        System.out.println("密码"+user.getPassword());
        ClientConnector connector = new ClientConnector("172.20.10.2", 8888);
        controller = new TeacherMainController(this, connector);

        mainVBox.setPrefHeight(500.0);
        mainVBox.setPrefWidth(802.0);

        // 创建AnchorPane
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setMaxHeight(Double.NEGATIVE_INFINITY);
        anchorPane.setMaxWidth(Double.NEGATIVE_INFINITY);
        anchorPane.setPrefHeight(500.0);
        anchorPane.setPrefWidth(802.0);
        AnchorPane.setTopAnchor(anchorPane, 0.0);
        AnchorPane.setBottomAnchor(anchorPane, 0.0);
        AnchorPane.setLeftAnchor(anchorPane, 0.0);
        AnchorPane.setRightAnchor(anchorPane, 0.0);

        // 创建背景图片
        ImageView backgroundImage = new ImageView();
        try {
            Image bgImage = new Image("background.png");
            backgroundImage.setImage(bgImage);
        } catch (Exception e) {
            System.out.println("无法加载背景图片: " + e.getMessage());
        }
        backgroundImage.setFitHeight(500.0);
        backgroundImage.setFitWidth(802.0);
        backgroundImage.setLayoutY(55.0);
        backgroundImage.setPreserveRatio(true);
        backgroundImage.setPickOnBounds(true);

        // 创建左侧半透明矩形
        Rectangle leftRect = new Rectangle();
        leftRect.setArcHeight(5.0);
        leftRect.setArcWidth(5.0);
        leftRect.setFill(Color.web("#d5e6f6"));
        leftRect.setHeight(432.0);
        leftRect.setLayoutY(68.0);
        leftRect.setOpacity(0.51);
        leftRect.setStroke(Color.BLACK);
        leftRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        leftRect.setWidth(233.0);

        // 创建顶部蓝色矩形
        Rectangle topRect = new Rectangle();
        topRect.setArcHeight(5.0);
        topRect.setArcWidth(5.0);
        topRect.setFill(Color.web("#044684"));
        topRect.setHeight(68.0);
        topRect.setStroke(Color.BLACK);
        topRect.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
        topRect.setWidth(802.0);

        // 创建标题文本
        Text titleText = new Text("虚拟校园系统");
        titleText.setFill(Color.WHITE);
        titleText.setLayoutX(321.0);
        titleText.setLayoutY(46.0);
        titleText.setStrokeWidth(0.0);
        titleText.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        titleText.setFont(Font.font("System", FontWeight.BOLD, 30.0));

        // 创建修改密码按钮
        changePasswordBtn.setLayoutX(20.0);
        changePasswordBtn.setLayoutY(460.0);
        changePasswordBtn.setMnemonicParsing(false);

        // 创建退出登录按钮
        logoutBtn.setLayoutX(155.0);
        logoutBtn.setLayoutY(460.0);
        logoutBtn.setMnemonicParsing(false);

        // 创建校园银行HBox
        HBox bankHBox = new HBox();
        bankHBox.setAlignment(Pos.CENTER);
        bankHBox.setLayoutX(1.0);
        bankHBox.setLayoutY(352.0);
        bankHBox.setPrefHeight(42.0);
        bankHBox.setPrefWidth(231.0);
        bankHBox.setSpacing(10.0);

        // 校园银行图标
        ImageView bankIcon = new ImageView();
        try {
            Image bankImage = new Image("bank.png");
            bankIcon.setImage(bankImage);
        } catch (Exception e) {
            System.out.println("无法加载校园银行图标: " + e.getMessage());
        }
        bankIcon.setFitHeight(58.0);
        bankIcon.setFitWidth(66.0);
        bankIcon.setPreserveRatio(true);
        bankIcon.setPickOnBounds(true);

        // 校园银行按钮
        bankBtn.setAlignment(Pos.CENTER);
        bankBtn.setMnemonicParsing(false);
        bankBtn.setPrefHeight(42.0);
        bankBtn.setPrefWidth(135.0);
        bankBtn.setStyle("-fx-background-color: #1E90FF;");
        bankBtn.setTextFill(Color.WHITE);
        bankBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        bankBtn.setOnAction(e -> {
            try {
                showBankFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        bankHBox.getChildren().addAll(bankIcon, bankBtn);

        // 创建教师信息HBox
        HBox teacherInfoHBox = new HBox();
        teacherInfoHBox.setAlignment(Pos.CENTER);
        teacherInfoHBox.setLayoutY(87.0);
        teacherInfoHBox.setPrefHeight(48.0);
        teacherInfoHBox.setPrefWidth(233.0);
        teacherInfoHBox.setSpacing(10.0);

        // 教师信息图标
        ImageView teacherInfoIcon = new ImageView();
        try {
            Image teacherInfoImage = new Image("teacher.png");
            teacherInfoIcon.setImage(teacherInfoImage);
        } catch (Exception e) {
            System.out.println("无法加载教师信息图标: " + e.getMessage());
        }
        teacherInfoIcon.setFitHeight(58.0);
        teacherInfoIcon.setFitWidth(66.0);
        teacherInfoIcon.setPreserveRatio(true);
        teacherInfoIcon.setPickOnBounds(true);

        // 教师信息按钮
        teacherInfoBtn.setAlignment(Pos.CENTER);
        teacherInfoBtn.setMnemonicParsing(false);
        teacherInfoBtn.setPrefHeight(42.0);
        teacherInfoBtn.setPrefWidth(135.0);
        teacherInfoBtn.setStyle("-fx-background-color: #1E90FF;");
        teacherInfoBtn.setTextFill(Color.WHITE);
        teacherInfoBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        teacherInfoBtn.setOnAction(e->{
            try {
                showTeacherInfoFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        

        teacherInfoHBox.getChildren().addAll(teacherInfoIcon, teacherInfoBtn);

        // 创建课程系统HBox
        HBox courseSystemHBox = new HBox();
        courseSystemHBox.setAlignment(Pos.CENTER);
        courseSystemHBox.setLayoutX(1.0);
        courseSystemHBox.setLayoutY(152.0);
        courseSystemHBox.setPrefHeight(42.0);
        courseSystemHBox.setPrefWidth(231.0);
        courseSystemHBox.setSpacing(10.0);

        // 课程系统图标
        ImageView courseSystemIcon = new ImageView();
        try {
            Image courseSystemImage = new Image("teaching.png");
            courseSystemIcon.setImage(courseSystemImage);
        } catch (Exception e) {
            System.out.println("无法加载课程系统图标: " + e.getMessage());
        }
        courseSystemIcon.setFitHeight(58.0);
        courseSystemIcon.setFitWidth(66.0);
        courseSystemIcon.setPreserveRatio(true);
        courseSystemIcon.setPickOnBounds(true);

        // 课程系统按钮
        courseSystemBtn.setAlignment(Pos.CENTER);
        courseSystemBtn.setMnemonicParsing(false);
        courseSystemBtn.setPrefHeight(42.0);
        courseSystemBtn.setPrefWidth(135.0);
        courseSystemBtn.setStyle("-fx-background-color: #1E90FF;");
        courseSystemBtn.setTextFill(Color.WHITE);
        courseSystemBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        courseSystemBtn.setOnAction(e -> {
            try {
                showTeacherCourseFrame();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        courseSystemHBox.getChildren().addAll(courseSystemIcon, courseSystemBtn);

        // 创建图书馆系统HBox
        HBox libraryHBox = new HBox();
        libraryHBox.setAlignment(Pos.CENTER);
        libraryHBox.setLayoutX(1.0);
        libraryHBox.setLayoutY(218.0);
        libraryHBox.setPrefHeight(36.0);
        libraryHBox.setPrefWidth(231.0);
        libraryHBox.setSpacing(10.0);

        // 图书馆系统图标
        ImageView libraryIcon = new ImageView();
        try {
            Image libraryImage = new Image("library.png");
            libraryIcon.setImage(libraryImage);
        } catch (Exception e) {
            System.out.println("无法加载图书馆系统图标: " + e.getMessage());
        }
        libraryIcon.setFitHeight(58.0);
        libraryIcon.setFitWidth(66.0);
        libraryIcon.setPreserveRatio(true);
        libraryIcon.setPickOnBounds(true);

        // 图书馆系统按钮
        libraryBtn.setAlignment(Pos.CENTER);
        libraryBtn.setMnemonicParsing(false);
        libraryBtn.setPrefHeight(42.0);
        libraryBtn.setPrefWidth(135.0);
        libraryBtn.setStyle("-fx-background-color: #1E90FF;");
        libraryBtn.setTextFill(Color.WHITE);
        libraryBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        libraryBtn.setOnAction(e -> {
            try {
                showLibraryFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        libraryHBox.getChildren().addAll(libraryIcon, libraryBtn);

        // 创建商店系统HBox
        HBox storeHBox = new HBox();
        storeHBox.setAlignment(Pos.CENTER);
        storeHBox.setLayoutX(1.0);
        storeHBox.setLayoutY(281.0);
        storeHBox.setPrefHeight(48.0);
        storeHBox.setPrefWidth(231.0);
        storeHBox.setSpacing(10.0);

        // 商店系统图标
        ImageView storeIcon = new ImageView();
        try {
            Image storeImage = new Image("shop.png");
            storeIcon.setImage(storeImage);
        } catch (Exception e) {
            System.out.println("无法加载商店系统图标: " + e.getMessage());
        }
        storeIcon.setFitHeight(58.0);
        storeIcon.setFitWidth(66.0);
        storeIcon.setPreserveRatio(true);
        storeIcon.setPickOnBounds(true);

        // 商店系统按钮
        storeBtn.setAlignment(Pos.CENTER);
        storeBtn.setMnemonicParsing(false);
        storeBtn.setPrefHeight(42.0);
        storeBtn.setPrefWidth(135.0);
        storeBtn.setStyle("-fx-background-color: #1E90FF;");
        storeBtn.setTextFill(Color.WHITE);
        storeBtn.setFont(Font.font("System", FontWeight.BOLD, 18.0));
        storeBtn.setOnAction(e -> {
            try {
                showStoreFrame();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        storeHBox.getChildren().addAll(storeIcon, storeBtn);

        // 创建校徽图标
        ImageView schoolBadgeIcon = new ImageView();
        try {
            Image schoolBadgeImage = new Image("badge.png");
            schoolBadgeIcon.setImage(schoolBadgeImage);
        } catch (Exception e) {
            System.out.println("无法加载校徽图标: " + e.getMessage());
        }
        schoolBadgeIcon.setFitHeight(58.0);
        schoolBadgeIcon.setFitWidth(51.0);
        schoolBadgeIcon.setLayoutX(14.0);
        schoolBadgeIcon.setLayoutY(9.0);
        schoolBadgeIcon.setPreserveRatio(true);
        schoolBadgeIcon.setPickOnBounds(true);

        // 将所有组件添加到AnchorPane
        anchorPane.getChildren().addAll(
                backgroundImage, leftRect, topRect, titleText,
                changePasswordBtn, logoutBtn, bankHBox, teacherInfoHBox,
                courseSystemHBox, libraryHBox, storeHBox, schoolBadgeIcon
        );

        // 将AnchorPane添加到主VBox
        mainVBox.getChildren().add(anchorPane);

        // 创建场景并显示
        Scene scene = new Scene(mainVBox);
        primaryStage.setTitle("虚拟校园系统 - 教师版");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void setChangePasswordButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        changePasswordBtn.setOnAction(handler::accept);
    }

    public void setLogoutButtonAction(java.util.function.Consumer<javafx.event.ActionEvent> handler) {
        logoutBtn.setOnAction(handler::accept);
    }

    public void showLoginFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) logoutBtn.getScene().getWindow();

        // 创建主界面
        LoginFrame loginFrame = new LoginFrame();
        Stage loginStage = new Stage();

        // 显示主界面
        loginFrame.start(loginStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showPasswordChangeFrame() throws IOException {
        // 获取当前登录窗口的Stage
        Stage registerStage = (Stage) logoutBtn.getScene().getWindow();

        // 创建主界面
        PasswordChangeFrame passwordChangeFrame = new PasswordChangeFrame();
        passwordChangeFrame.setPassword(user.getPassword());
        passwordChangeFrame.setUsername(user.getId());
        passwordChangeFrame.setRole("教师");
        Stage passwordChangeStage = new Stage();

        // 显示主界面
        passwordChangeFrame.start(passwordChangeStage);

        // 关闭登录窗口
        registerStage.close();
    }

    public void showTeacherCourseFrame() throws IOException {
        Stage registerStage = (Stage) courseSystemBtn.getScene().getWindow();
        TeacherCourseFrame teacherCourseFrame = new TeacherCourseFrame();
        teacherCourseFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage teacherCourseStage = new Stage();

        teacherCourseFrame.start(teacherCourseStage);
        registerStage.close();
    }

    public void showBankFrame() throws Exception {
        Stage registerStage = (Stage) bankBtn.getScene().getWindow();
        BankFrame bankFrame = new BankFrame();
        bankFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage bankStage = new Stage();

        bankFrame.start(bankStage);
        registerStage.close();
    }

    public void showLibraryFrame() throws Exception {
        Stage registerStage = (Stage) libraryBtn.getScene().getWindow();
        LibraryFrame libraryFrame = new LibraryFrame();
        libraryFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage libraryStage = new Stage();

        libraryFrame.start(libraryStage);
        registerStage.close();
    }

    public void showStoreFrame() throws Exception {
        Stage registerStage = (Stage) storeBtn.getScene().getWindow();
        ShopFrame shopFrame = new ShopFrame();
        shopFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage storeStage = new Stage();

        shopFrame.start(storeStage);
        registerStage.close();
    }

    public void showTeacherInfoFrame() throws Exception {
        Stage registerStage = (Stage) teacherInfoBtn.getScene().getWindow();
        TeacherInfoFrame teacherInfoFrame = new TeacherInfoFrame();
        teacherInfoFrame.setUser(user.getId(),user.getPassword(),user.getRole());
        Stage storeStage = new Stage();

        teacherInfoFrame.start(storeStage);
        registerStage.close();
    }

}